<?php

declare(strict_types=1);

namespace app\customer\controller;

use app\customer\controller\Base;
use think\Request;

/**
 * 客户控制器
 */
class Customer extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $admin_id;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\customer\service\Customer();
        $this->validate = new \app\customer\validate\Customer();
        $this->admin_id = 9; // TODO: 临时写死的
    }

    /**
     * @describe 获取客户列表
     * @author: Bobo
     * @date: 2024-06-05 03:04:33 
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }

        $pageIndex  = isset($params['pageIndex'])    ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])     ? intval($params['pageSize']) : 10;
        $cusname    = isset($params['cusname'])      ? intval($params['cusname']) : null;
        $status     = isset($params['status'])       ? intval($params['status']) : null;
        $stage      = isset($params['stage'])        ? intval($params['stage']) : null;
        $cusnature  = isset($params['cusnature'])    ? intval($params['cusnature']) : null;
        $createTime = isset($params['create_time'])  ? trim($params['create_time']) : null;
        $updateTime = isset($params['update_time'])  ? trim($params['update_time']) : null;


        $where = ['is_del' => 0, 'owner' => $this->admin_id];
        if ($cusname) {
            $where[] = ['cusname', 'like', '%' . $cusname . '%'];
        }
        if ($status > -1) {
            $where['status'] = $status;
        }
        if ($stage > -1) {
            $where['stage'] = $stage;
        }
        if ($cusnature) {
            $where['cusnature'] = $cusnature;
        }
        if ($createTime) {
            $where['create_time'] = $createTime;
        }
        if ($updateTime) {
            $where['update_time'] = $updateTime;
        }

        $res =  $this->service->getList($pageIndex, $pageSize, $where);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }
    /**
     * @describe 获取客户详情 
     * @author: Bobo
     * @date: 2024-06-05 03:07:37 
     * @param [int] $id 客户ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }
        $id = intval($params['id']);
        $res  = $this->service->getOneById($id);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }

    /**
     * @describe 创建客户
     * @author: Bobo
     * @date: 2024-06-05 03:09:17 
     * @param [array] $data 创建客户的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('create')->check($params)) {
            return $this->returnNewJsonData(100001, null, $this->validate->getError());
        }
        $params['creator'] = 1;
        $res  = $this->service->create($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }

    /**
     * 更新客户
     * @describe 更新客户
     * @author: Bobo
     * @date: 2024-06-05 03:11:17 
     * @param [int] $id 客户ID @required
     * @param [array] $data 更新客户的数据 @required
     * @return Renderable
     */
    public function update()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }
        return json($this->service->update($params));
    }


    /**
     * @describe 删除客户
     * @author: Bobo
     * @date: 2024-06-05 03:13:56 
     * @param [int] $id 客户ID @required
     * @return Renderable
     */
    public function delete()
    {
        $ids = $this->request->post('ids');
        if (!$this->validate->scene('delete')->check($ids)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }

        $result = $this->service->batchLogicalDelete($ids);
        return $this->returnJsonData($result);
    }

    /**
     * @describe 获取公海客户列表
     * @author: Bobo
     * @date: 2024-06-05 03:04:33 
     * @param [] type
     * @return Renderable
     */
    public function publicIndex()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }

        $pageIndex  = isset($params['pageIndex'])    ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])     ? intval($params['pageSize']) : 10;
        $cusname    = isset($params['cusname'])      ? intval($params['cusname']) : null;
        $status     = isset($params['status'])       ? intval($params['status']) : null;
        $stage      = isset($params['stage'])        ? intval($params['stage']) : null;
        $cusnature  = isset($params['cusnature'])    ? intval($params['cusnature']) : null;
        $createTime = isset($params['create_time'])  ? trim($params['create_time']) : null;
        $updateTime = isset($params['update_time'])  ? trim($params['update_time']) : null;

        $creator = 1;

        $where = ['is_del' => 0, 'owner' => 0];
        if ($cusname) {
            $where[] = ['cusname', 'like', '%' . $cusname . '%'];
        }
        if ($status > -1) {
            $where['status'] = $status;
        }
        if ($stage > -1) {
            $where['stage'] = $stage;
        }
        if ($cusnature) {
            $where['cusnature'] = $cusnature;
        }
        if ($createTime) {
            $where['create_time'] = $createTime;
        }
        if ($updateTime) {
            $where['update_time'] = $updateTime;
        }

        $res =  $this->service->getList($pageIndex, $pageSize, $where);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }

    /**
     * @describe 分配公海客户
     * @author: Bobo
     * @date: 2024-07-05 14:14:02 
     * @param [type] @required
     * @return Renderable
     */
    public function allocationPublicCustomer()
    {
        $params = $this->request->params;

        if (!$this->validate->scene('allocation')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }
        $res = $this->service->batchUpdate(
            ['id' => $params['id']],
            ['owner' => $params['allocationId'], 'updater' => $this->admin_id]
        );

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }

    /**
     * @describe 捞取公海客户
     * @author: Bobo
     * @date: 2024-07-05 14:20:07
     * @param [type] @required
     * @return Renderable
     */
    public function fishingPublicCustomer()
    {
        $params = $this->request->params;

        if (!isset($params['id'])) {
            return $this->returnNewJsonData(100001, [], " The 'id' parameter is required.");
        }

        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }

        $res = $this->service->batchUpdate(
            ['id' => $params['id']],
            ['owner' => $this->admin_id, 'updater' => $this->admin_id, 'update_time' => date('Y-m-d H:i:s')]
        );

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }

    /**
     * @describe 退回客户到公海   
     * @author: Bobo
     * @date: 2024-07-11 14:19:54 
     * @param [type] @required
     * @return Renderable
     */
    public function returnPublic()
    {
        $params = $this->request->params;
        if (!isset($params['id'])) {
            return $this->returnNewJsonData(100001, [], " The 'id' parameter is required.");
        }

        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnNewJsonData(100001, [], $this->validate->getError());
        }

        $res = $this->service->batchUpdate(
            ['id' => $params['id']],
            ['owner' => 0, 'updater' => $this->admin_id, 'update_time' => date('Y-m-d H:i:s')]
        );

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnNewJsonData($code, $data, $message);
    }
}
